<?php
   class Model_package extends CI_Model{
		protected $_table = "tbl_package";
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		public function getdata($id){
			$this->db->where("id",$id);
			return $this->db->get($this->_table)->row_array();
		}
		public function getcolunm_allpackage($column){
			$this->db->select($column);
			return $this->db->get($this->_table)->result_array();
		}
		public function listall($limit,$start){
			$this->db->limit($limit,$start);
			return $this->db->get($this->_table)->result_array();
		}
		public function listall_name(){
			$this->db->select("title");
			return $this->db->get($this->_table)->result_array();
		}
		public function add($data){
        	$this->db->insert($this->_table,$data);
		}
		public function del($id){
			$this->db->where("id",$id);
			$this->db->delete($this->_table);
		}
		public function update($data,$id){
			$this->db->where("id",$id);
			$this->db->update($this->_table,$data);
		}
       function package_search($title, $start=null, $limit=null) {
           return $this->db
               ->like('title', $title)
               ->limit($start, $limit)
               ->get($this->_table)->result_array();
       }
       function count_package_search($title) {
            $this->db
               ->where('title',$title)
               ->count_all_results($this->_table);
       }
       public function search($name,$cate_id, $tur, $gia, $numberget, $start)
       {
           $where = "";
           $where .= "category_id = {$cate_id} ";
           if(isset($name) && $name != '') {
               $name = explode(" ", $name);
               $where .= 'AND';
               foreach ($name as $index => $keyword)
               {
                   $where .= " en_name like '%{$keyword}%' AND ";
               }
               $where = trim($where);
               if (strlen($where) > 4)
               {
                   $where = substr($where, 0, -4);
               }
           }
           if(isset($gia) && $gia != ''){
               $where .= "AND {$gia} ";
           }
           if(isset($tur) && $tur != '') {
               $where .= "AND {$tur} ";
           }
           $where = trim($where);

           $data = $this->db
               ->order_by("id", 'desc')
               ->where($where)
               ->get($this->_table, $numberget, $start)
               ->result_array();
           return $data;
       }
       function total($name,$cate_id, $tur, $gia) {
           $where = "";
           $where .= "category_id = {$cate_id} ";
           if(isset($name) && $name != '') {
               $where .= "AND en_name like '%{$name}%' ";
           }
           if(isset($gia) && $gia != ''){
               $where .= "AND {$gia} ";
           }
           if(isset($tur) && $tur != '') {
               $where .= "AND {$tur} ";
           }
           $where = trim($where);
           return $this->db->where($where)->count_all_results($this->_table);
       }
	   function datasearch($provide,$prefer,$orderby,$limit,$start){
		   $select="select *,(select pricefor1 from tbl_packageprice where packageid=h.id order by pricefor1 limit 0,1) as price from tbl_package as h ";
		   $where=" where destination like '%".$provide."%' ";
		   if($prefer!=0) $where.=" and category=".$prefer;
		   $order=" order by ".$orderby;
		   $max="  limit ".$start.",".$limit;
		   $query=$this->db->query($select.$where.$order.$max);
		   //var_dump($select.$where.$order.$max);
		   return $query->result_array();
	   }
	   function getminprice_detail($id,$datefrom){
			$query=$this->db->query("select *,(select pricefor1 from tbl_packageprice where packageid=".$id." and datefrom <='".$datefrom."' and dateto >='".$datefrom."' order by pricefor1 limit 0,1) as price from tbl_package where id=".$id); 			
			return $query->row_array();
		}
	   
	   function gettopByprovide_status($provide,$status,$limit,$start){
		   $query=$this->db->query("select * from tbl_package where destination like '%".$provide."%' and status=".$status." order by recommendations limit ".$start.",".$limit);
		   return $query->result_array();
	   }
	   function getallbyprovide_name($provide,$title){
		    $where=" where 1=1 ";
			$select="select * from tbl_package ";
			$order=" order by title ";
			$query=$this->db->query($select.$where.$provide.$title.$order);   			
			return $query->result_array();
	   }
	   
	   function countbycate($cate){
			$query=$this->db->query("select count(*) as total from tbl_package where category=".$cate);   
			return $query->row_array();
	   }
	}
?>